<template>
  <div class="paihangbang" ref="paihangbangDom">
    <GlobalTitle title="排行榜" class="w" @titleClick="toToplistPage" />
    <div class="content" v-show="details.length === 0">
      <div class="w contentBox">
        <div class="content_left">
          <el-skeleton animated :throttle="500">
            <template slot="template">
              <el-skeleton-item variant="image" style="height: 213px" />
              <el-skeleton-item
                variant="p"
                style="height: 20px; width: 224px; margin: 10px 0"
              />
            </template>
          </el-skeleton>
        </div>

        <div class="content_right">
          <el-skeleton animated :throttle="500">
            <template slot="template">
              <el-skeleton-item
                variant="p"
                style="height: 80px; margin: 0 0 10px 0"
              />
              <el-skeleton-item
                variant="p"
                style="height: 80px; margin: 0 0 10px 0"
              />
              <el-skeleton-item
                variant="p"
                style="height: 80px; margin: 0 0 10px 0"
              />
              <el-skeleton-item
                variant="p"
                style="height: 80px; margin: 0 0 10px 0"
              />
            </template>
          </el-skeleton>
        </div>
      </div>
    </div>
    <div class="content" v-show="details.length !== 0">
      <div class="w contentBox">
        <div class="content_left">
          <el-carousel
            type="card"
            style="width: 100%"
            indicator-position="none"
            :autoplay="false"
            @change="carouselChange"
          >
            <el-carousel-item
              v-for="item in $constant.ToplistTeseBang"
              :key="item.id"
            >
              <img :src="item.url" alt="" />
            </el-carousel-item>
          </el-carousel>
          <div class="name">{{ activeItem.name }}</div>
          <p>{{ activeItem.time }}</p>
          <button>
            <i class="el-icon-video-play"></i>
            播放歌单
          </button>
        </div>

        <div class="content_right">
          <div
            ref="wrapper"
            class="wrapper"
            style="overflow: hidden; height: 100%; overflow-y: auto"
          >
            <ul class="content">
              <li v-for="(item, index) in details" :key="item.id">
                <div class="num">
                  {{ index + 1 &lt; 10 ? "0" + (index + 1) : index + 1 }}
                </div>
                <div class="pic">
                  <GlobalImageDeep
                    :url="item.al.picUrl"
                    :imgWidth="66"
                    :imgHeight="66"
                  />
                  <!-- 播放按钮 -->
                  <GlobalPlayMask :maskWidth="30" :maskHeight="30" />
                </div>
                <div
                  class="name one_txt_cut"
                  @click="toSongDetailsPage(item.id)"
                >
                  {{ item.name }}
                </div>
                <div class="time">
                  {{ formatTime(item.dt) }}
                </div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from "@better-scroll/core";
import { getPaiHangBangDetail } from "../../../../../api/homeApi";
import ObserverDomToScreen from "../../../../../utils/ObserverDomToScreen.js";
import moment from "moment";
export default {
  name: "paihangbang",
  components: {},
  data() {
    return {
      activeItem: {},
      details: [],
    };
  },
  created() {
    // 默认让他 显示 飙升榜
    this.activeItem = this.$constant.ToplistTeseBang[0];
  },
  mounted() {
    this.scroll = new BScroll(this.$refs.wrapper, {
      bounce: false,
      scrollY: true,
    });

    // DOM到达可视区域，才请求异步接口
    ObserverDomToScreen(this.$refs.paihangbangDom, this.getPaiHangBangDetail);
  },
  methods: {
    // 轮播图发生变化的时候
    carouselChange(newIndex) {
      // console.log(newIndex);

      // this.details = [];
      this.activeItem = this.$constant.ToplistTeseBang[newIndex];
      this.getPaiHangBangDetail(this.activeItem.id);

      // 确保右侧 滚动条，滚动到最上方\
      console.log(this.scroll);
      this.$nextTick(() => {
        // this.scroll.scrollTo(0, 200, 200);
        this.$refs.wrapper.scroll(0, 0);
      });
      // console.log(this.scroll.maxScrollY);
      // this.scroll.hooks.on("refresh", () => {
      //   console.log("refreshed");
      // });
    },
    // 请求轮播图的详情
    async getPaiHangBangDetail(id) {
      try {
        const result = await getPaiHangBangDetail(id);
        if (result.code === 200) {
          this.details = result.playlist.tracks.slice(0, 15);
          console.log("排行榜详情", this.details);
        }
      } catch (error) {}
    },
    // 格式化时间
    formatTime(num) {
      // 毫秒
      return moment(num).format("mm:ss");
    },
    toSongDetailsPage(id) {
      this.$router.push(`/songDetails/${id}`);
    },
    toToplistPage() {
      this.$router.push("/toplist");
    },
  },
  computed: {},
};
</script>

<style lang="less" scoped>
.paihangbang {
  /deep/ .el-carousel__container {
    height: 213px;
  }
  /deep/ .el-carousel__item {
    img {
      width: 100%;
      height: 100%;
      border-radius: 8px;
    }
  }
  /deep/ .el-image {
    border-radius: 8px;
  }
  .content {
    height: 474px;
    background-color: #f2f2f2;
    // padding: 20px 0;
    // 真正的内容盒子
    .contentBox {
      display: flex;
      height: 474px;
      .content_left {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 400px;
        height: 100%;
        margin-right: 30px;
        .name {
          margin-top: 20px;
          font-size: 24px;
          color: #333333;
          font-weight: bold;
          cursor: pointer;
          letter-spacing: 5px;
          &:hover {
            color: @globalColor;
          }
        }
        p {
          margin-top: 15px;
          font-size: 14px;
          color: #333333;
          opacity: 0.3;
        }
        button {
          position: relative;
          width: 140px;
          height: 40px;
          // text-align: center;
          padding-left: 28px;
          line-height: 38px;
          border: 1px solid #333;
          border-radius: 20px;
          margin: 22px auto;
          box-sizing: border-box;
          font-weight: 700;
          cursor: pointer;
          font-size: 16px;
          i {
            position: absolute;
            left: 22px;
            top: 10px;

            font-size: 22px;
          }
          &:hover {
            color: @globalColor;
          }
        }
      }
      .content_right {
        flex: 1;
        padding: 20px 10px;
        .wrapper ul li {
          display: flex;
          align-items: center;
          height: 80px;

          border-radius: 8px;
          margin-bottom: 2px;
          font-size: 14px;
          color: #333333;
          &:hover {
            /deep/ .playMask .play {
              display: block;
              cursor: pointer;
            }
            background-color: #e6e6e6;
          }

          .num {
            width: 70px;
            text-align: center;
          }
          .pic {
            position: relative;
            margin-top: 13px;
            margin-right: 20px;
            // display: flex;
            // align-items: center;
            height: 100%;
          }
          .name {
            // width: calc(100% - 150px - 86px);
            // width: 100%;
            // flex: 1;
            width: 507px;
            // height: 30px;
            &:hover {
              color: @globalColor;
              cursor: pointer;
            }
          }
          .time {
            width: 80px;
            text-align: center;
          }
        }
      }
    }
  }
}
</style>
